import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import { Upload, Button, message } from 'antd';
import { DeleteOutlined } from '@ant-design/icons';
import axios from 'axios';
import styles from './index.less';

declare type Props = {
  name?: string;
  filename?: string; //文件名
  children?: React.Component;
  uuid: string;
  dispatch: Function;
};
const Download = ({ name, filename, dispatch, children, uuid }: Props) => {
  /**
   * 下载组件
   * @param param0
   */
  const downClick = () => {
    if (!uuid) {
      message.error('图片链接不存在');
      return;
    }
    axios({
      url: '/api/file/v1/download?uuid=' + uuid,
      method: 'get',
      responseType: 'arraybuffer',
      headers: {
        token: sessionStorage.getItem('token'),
      },
    })
      .then(res => {
        let blob = new Blob([res.data]);
        let link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = decodeURIComponent(name || filename);
        link.click();
        message.success('文件已下载');
      })
      .catch(() => {
        console.log('下载异常');
      });
  };
  return (
    <Button className={styles.filename} type="link" onClick={() => downClick()}>
      {children || name || filename}
    </Button>
  );
};
export default connect(state => state.base)(Download);
